<?php defined('SYSPATH') or die('No direct script access.');

class Controller_News extends Controller_Site {

	public function action_index()
	{
		$news=ORM::factory('news')->join('users')->on('news.user_id','=','users.id')->where('active', '=', 1)->order_by('date', 'desc')->find_all();
		$social = new Socialnetwork();
                $this->content=View::factory('news/index')->set('news',$news)->set('social', $social);
	}
	
	public function action_list()
	{
		$news=ORM::factory('news')->order_by('date')->find_all();
		$this->content=View::factory('news/list')->set('news',$news);
	}
	
	public function action_new() {
        $form = View::factory('news/form')->set('action', URL::site('news/new'));
        
        $ckeditor = new CKEditor();
        $config = array("toolbar" =>
        array(
                    array(
        'Bold', 'Italic', '-', 'NumberedList', 'BulletedList', '-', 'Link', 'Unlink', '-', 'Table'
                    )
                )
                );

        $form->set('ckeditor', $ckeditor)->set('config', $config);
        
        if($this->request->method() == 'POST') {
            try {
                $news = new Model_News();
                $form->set('news', $news);
                $this->processForm($news);

                $this->setFlash('notice', 'Sikeresen felvetted az új hírt!');
                $this->request->redirect('news/list');
            }
            catch (ORM_Validation_Exception $e) {
                $form->set('errors', $e->errors('models'));
            }
        }

        $this->content->set('form', $form);
    }

    protected function processForm($news) {
        $news->title = $this->request->post('title');
        $news->lead = $this->request->post('lead');
        $news->description = $this->request->post('description');
        $news->date = date('Y-m-d');
        $name = Session::instance()->get($name);
        $id = ORM::factory('users')->where('name', '=', $name)->find()->id;
        
        //CSAK A TESZTELÉSHEZ MÍG NINCS SSO
        
        if($id==NULL){
            $id=1;
        }
        
        if(($this->request->post('active'))=='on'){
            $news->active = 1;
        }
        else{
            $news->active = 0;
        }
        $news->user_id = $id;
        $news->save();
    }

     public function action_show() {
        $news = new Model_News($this->request->param('id'));
        $social = new Socialnetwork();
        $this->content->set('news', $news)->set('social', $social);
     }
    
    public function action_edit() {
        $form = View::factory('news/form')->set('action', URL::site('news/edit/' . $this->request->param('id')));
        $news = new Model_News($this->request->param('id'));
        
        $ckeditor = new CKEditor();
        $config = array("toolbar" =>
        array(
                    array(
        'Bold', 'Italic', '-', 'NumberedList', 'BulletedList', '-', 'Link', 'Unlink', '-', 'Table'
                    )
                )
                );

        $form->set('ckeditor', $ckeditor)->set('config', $config);

        if($this->request->method() == 'POST') {
            try {
                $this->processForm($news, true);
                $this->setFlash('notice', 'Sikeresen módosítottad a hírt!');
                $this->request->redirect('news/list');
            }
            catch (ORM_Validation_Exception $e) {
                $form->set('errors', $e->errors('models'));
            }
        }
        $form->set('news', $news);
        $this->content->set('form', $form);
    }
	
	public function action_delete()
	{
		$news = new Model_News($this->request->param('id'));
        try {
            $news->delete();
            $this->setFlash('notice', 'Sikeresen törölted a hírt!');
            $this->request->redirect('news/list');
        }
        catch(Kohana_Exception $e) {
            $this->setFlash('error', $e->getMessage());
        }
	}

}
